package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.pojo.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 添加套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save (@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return R.success("套餐添加成功");
    }
    @GetMapping("page")
    public R<Page<SetmealDto>> page(Integer page, Integer pageSize,String name){
        Page<Setmeal> pageInfo1=new Page<>(page,pageSize);
        QueryWrapper<Setmeal> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(name),"name",name);
        wrapper.orderByDesc("update_time");
        setmealService.page(pageInfo1,wrapper);
        Page<SetmealDto> pageInfo2 = new Page<>(page,pageSize);
        BeanUtils.copyProperties(pageInfo1, pageInfo2, "records");
        ArrayList<SetmealDto> setmealDtoList = new ArrayList<>();
        for (Setmeal setmeal : pageInfo1.getRecords()) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal,setmealDto);
            Category category = categoryService.getById(setmeal.getCategoryId());
            setmealDto.setCategoryName(category.getName());
            setmealDtoList.add(setmealDto);
        }
        pageInfo2.setRecords(setmealDtoList);
        return R.success(pageInfo2);
    }
    @DeleteMapping
    public R<String> deleteById(Long[] ids){
        setmealService.deleteByIds(ids);
        return R.success("删除成功");
    }
    @PostMapping("status/{status}")
    public R updateStatus(@PathVariable Integer status,Long[]ids){
        LambdaUpdateWrapper<Setmeal> wrapper = Wrappers
                .lambdaUpdate(Setmeal.class)
                .set(Setmeal::getStatus, status)
                .in(Setmeal::getId, ids);
        setmealService.update(wrapper);
        return R.success("修改成功");
    }
    @GetMapping("list")
    public R list(Long categoryId){
        LambdaQueryWrapper<Setmeal> wrapper = Wrappers
                .lambdaQuery(Setmeal.class)
                .eq(categoryId!=0,Setmeal::getCategoryId, categoryId)
                .orderByAsc(Setmeal::getUpdateTime);
        List<Setmeal> setmealList = setmealService.list(wrapper);
        return R.success(setmealList);
    }


}
